package hos.permission;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: PermissionChecker </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-01-05 22:29
 */
public final class PermissionChecker {

    public static final int REQUEST = 2;
    public static final int REFUSED = 1;
    public static final int SUCCESS = 0;

    private PermissionChecker() {
    }

    /**
     * 获取被拒绝的权限
     *
     * @param activity    上下文
     * @param permissions 需要检查的权限
     * @return 需要请求的权限
     */
    @NonNull
    public static List<String> getRefusedPermission(@NonNull Activity activity, @NonNull String[] permissions) {
        List<String> permissonList = new ArrayList<>();
        for (String item : permissions) {
            if (refused(activity, item)) {
                permissonList.add(item);
            }
        }
        return permissonList;
    }

    /**
     * 获取被拒绝的权限
     *
     * @param activity    上下文
     * @param permissions 需要检查的权限
     * @return 需要请求的权限
     */
    @NonNull
    public static List<String> getRefusedPermission(@NonNull Activity activity, @NonNull List<String> permissions) {
        List<String> permissonList = new ArrayList<>();
        for (String item : permissions) {
            if (refused(activity, item)) {
                permissonList.add(item);
            }
        }
        return permissonList;
    }

    /**
     * 获取需要请求的权限
     *
     * @param context     上下文
     * @param permissions 需要检查的权限
     * @return 需要请求的权限
     */
    @NonNull
    public static List<String> getRequestPermission(@NonNull Context context, @NonNull String[] permissions) {
        List<String> permissonList = new ArrayList<>();
        for (String item : permissions) {
            if (!have(context, item)) {
                permissonList.add(item);
            }
        }
        return permissonList;
    }


    /**
     * 获取需要请求的权限
     *
     * @param context     上下文
     * @param permissions 需要检查的权限
     * @return 需要请求的权限
     */
    @NonNull
    public static List<String> getRequestPermission(@NonNull Context context, @NonNull List<String> permissions) {
        List<String> permissonList = new ArrayList<>();
        for (String item : permissions) {
            if (!have(context, item)) {
                permissonList.add(item);
            }
        }
        return permissonList;
    }


    public static boolean have(@NonNull Context context, @NonNull String permission) {
        return ContextCompat.checkSelfPermission(context, permission) ==
                PackageManager.PERMISSION_GRANTED;
    }

    public static boolean refused(@NonNull Activity activity, @NonNull String permission) {
        return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
    }


}
